package com.nutricia.core.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.nutricia.common.util.OkHttpUtils;
import com.nutricia.core.ex.BizErrorCode;
import com.nutricia.core.ex.BizException;
import com.nutricia.core.model.domain.AccessTokenDO;
import com.nutricia.core.model.vo.ReturnTokenVO;
import com.nutricia.core.service.GetAccessTokenService;

@Service("getAccessTokenService")
public class GetAccessTokenServiceImpl extends AbstractServiceImpl implements GetAccessTokenService{

    @Value("${tokenParameter.corpID}")
    private String corpID;
    
    @Value("${tokenParameter.secret}")
    private String secret;
	
	/**
	 * 获取数据库token
	 */
	@Override
	public AccessTokenDO getAccessTokenByMySql() {
		logger.info("数据库获取token开始！");
		AccessTokenDO accessTokenDO = null;
		try {
			accessTokenDO = getAccessTokenMapper.getAccessToken();
		} catch (Exception e) {
			logger.error("数据库获取token异常:{GetAccessTokenServiceImpl.getAccessTokenByMySql}",e);
			throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("数据库获取token成功！");
		return accessTokenDO;
	}
	
	@Override
	public String getAccessToken(){
		logger.info("获取token开始！");
		String json = null;
		AccessTokenDO accessTokenDO = getAccessTokenByMySql();
		try {
			if(accessTokenDO == null) {
				//获取token
				String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpID+"&corpsecret="+secret;
				json = OkHttpUtils.get(url, null);
				ReturnTokenVO returnTokenVO = JSON.parseObject(json, ReturnTokenVO.class);
				
				//token入库
				AccessTokenDO accessTokenDO2 = new AccessTokenDO();
				accessTokenDO2.setAccessToken(returnTokenVO.getAccess_token());
				accessTokenDO2.setCreatetime(System.currentTimeMillis()+"");
				getAccessTokenMapper.addAccessToken(accessTokenDO2);
				
				logger.info("获取token成功！");
				return returnTokenVO.getAccess_token();
			}else {
				long longTimeNew = System.currentTimeMillis();
				long longTimeOld = Long.parseLong(accessTokenDO.getCreatetime());
				//失效
				if(longTimeNew - longTimeOld > 7100) {
					//获取token
					String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpID+"&corpsecret="+secret;
					json = OkHttpUtils.get(url, null);
					ReturnTokenVO returnTokenVO = JSON.parseObject(json, ReturnTokenVO.class);
					//修改token
					AccessTokenDO accessTokenDO2 = new AccessTokenDO();
					accessTokenDO2.setTokenId(accessTokenDO.getTokenId());
					accessTokenDO2.setAccessToken(returnTokenVO.getAccess_token());
					accessTokenDO2.setCreatetime(System.currentTimeMillis()+"");
					
					getAccessTokenMapper.updateAccessToken(accessTokenDO2);
					logger.info("获取token成功！");
					return returnTokenVO.getAccess_token();
				//没失效	
				}else {
					logger.info("获取token成功！");
					return accessTokenDO.getAccessToken();
				}
			}
		} catch (Exception e) {
			logger.error("获取token异常:{GetAccessTokenServiceImpl.getAccessToken}",e);
			throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
	}

}
